﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SalePromotion.Extensions;
using SalePromotion.eBay;
using eBay.Service.Core.Soap;

namespace SalePromotion.Models
{
    public partial class SaleModel
    {
        public SaleModel():this( new Sale(){ StartDate =DateTime.Now.AddHours(1) ,EndDate =DateTime.Now.AddDays(1) })
        {

        }
        public SaleModel(Sale sale)
        {
            this.SaleName = sale.SaleName;
            this.StartDate = sale.StartDate;

            this.EndDate = sale.EndDate;


            this.IsAuctionExcluded = sale.IsAuctionExcluded;
            this.DiscountRatio = sale.DiscountRatio;
            this.DiscountType = sale.DiscountType;


            this.IsFreeShipping = sale.IsFreeShipping;
            this.AllAuctionItems = sale.AllAuctionItems;
            this.AllFixedPriceItems = sale.AllFixedPriceItems;
            this.AllStoreInventoryItems = sale.AllStoreInventoryItems;
            this.CategoryId = sale.CategoryID;
            this.StoreCategoryId = sale.StoreCatetoryID;
            this.Status = sale.Status;
            this.Id = sale.Id;
            this.PromotionSaleId = sale.PromotionSaleID;
            
            Init();
            InitSaleListings(sale.SaleListings);
        }

        private void InitSaleListings(EntityCollection<SaleListing> entityCollection)
        {
            if(entityCollection != null && entityCollection.Count >0)
            {
                SelectedListingArray = entityCollection.Select(entity => entity.ItemId);
               
            }
        }


        [Required]
        [Display(Name = "Sale name")]
        [StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 1)]
        public string SaleName { get; set; }

        public DateTime StartDate { get; set; }


        [Required]
        [DataType(DataType.DateTime)]
        [Display(Name = "Start Date")]
        public DateTime StartDateDate { get; set; }
        public string StartDateHour { get; set; }
        public string StartDateMinute { get; set; }


        public string EndDateHour { get; set; }
        public string EndDateMinute { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        [Display(Name = "End Date")]

        public DateTime EndDateDate { get; set; }
        public DateTime EndDate { get; set; }

        public bool IsAuctionExcluded { get; set; }

        public decimal DiscountRatio { get; set; }


        public decimal DiscountRatioByPercent { get; set; }

        public decimal DiscountRatioFixed { get; set; }

        public int DiscountType { get; set; }
        public bool IsDiscountByPrice { get; set; }

        public bool IsFreeShipping { get; set; }
        public bool AllAuctionItems { get; set; }
        public bool AllFixedPriceItems { get; set; }
        public bool AllStoreInventoryItems { get; set; }
        public long? CategoryId { get; set; }
        public long? StoreCategoryId { get; set; }

        public string Status { get; set; }


        public int? Id { get; set; }
        public long? PromotionSaleId { get; set; }


        public string FreeShippingText
        {
            get
            {
                return IsFreeShipping ? "Free" : "--";
            }

        }

        public string DiscountText
        {
            get
            {
                if ((DiscountType) == (int)DiscountCodeType.Price)
                {
                    return "$" + DiscountRatio.ToString("#,###.00");
                }
                else
                {
                    return DiscountRatio.ToString("#,###") + "%" + " Off";
                }
            }
        }

        public string StatusText
        {
            get { return StartDate.GetStatus(EndDate); }
        }

        public IEnumerable<SelectListItem> StoreCategories
        {
            get { return new eBaySession().GeteBayStoreCategories().ToSelectListItems(); }
        }
        public IEnumerable<SelectListItem> Hours { get; set; }
        public IEnumerable<SelectListItem> Minutes { get; set; }

        public string SelectedListings { get; set; }
        
        public IEnumerable<string> SelectedListingArray
        {
            get
            {
                if (_selectedListingArray == null || !_selectedListingArray.Any())
                {
                    if(!string.IsNullOrWhiteSpace(SelectedListings))
                    {
                        _selectedListingArray = SelectedListings.Split(new char[] {','});
                    }
                  
                }
                return _selectedListingArray;

            }
            set
            {
                _selectedListingArray = value;
                if (_selectedListingArray != null && _selectedListingArray.Any())
                {
                    SelectedListings = SelectedListingArray.Aggregate((a, b) => a + "," + b);
                }

            }
        }

        private IEnumerable<string> _selectedListingArray;

        public long TotalSelectedListings 
        {
            get { return SelectedListingArray.Count(); }
        }

        public string Message { get; set; }

        private void Init()
        {

            StartDateHour = this.StartDate.Hour.ToString().PadLeft(2, '0');
            StartDateMinute = this.StartDate.Minute.ToString().PadLeft(2, '0');
            StartDateDate = StartDate.Date;

            EndDateHour = this.EndDate.Hour.ToString().PadLeft(2, '0');
            EndDateMinute = this.EndDate.Minute.ToString().PadLeft(2, '0');
            EndDateDate = EndDate.Date;


            if ((DiscountType) == (int)DiscountCodeType.Price)
            {
                IsDiscountByPrice = true;
                this.DiscountRatioFixed = DiscountRatio;
            }
            else
            {
                IsDiscountByPrice = false;
                this.DiscountRatioByPercent = DiscountRatio;
            }

            SelectedListingArray =new List<string>();
            Hours = 24.GetHourOrMinutes();
            Minutes = 60.GetHourOrMinutes();

        }


        internal void RestoreFromSession(SaleModel saleModelFromSession)
        {
            this.SelectedListingArray = saleModelFromSession.SelectedListingArray;

            this.SaleName = saleModelFromSession.SaleName;
            StartDateDate = saleModelFromSession.StartDateDate;
            StartDateHour = saleModelFromSession.StartDateHour;
            StartDateMinute = saleModelFromSession.StartDateMinute;

            EndDateDate = saleModelFromSession.EndDateDate;
            EndDateHour = saleModelFromSession.EndDateHour;
            EndDateMinute = saleModelFromSession.EndDateMinute;

            IsAuctionExcluded = saleModelFromSession.IsAuctionExcluded;
            DiscountType = saleModelFromSession.DiscountType;
            DiscountRatioByPercent = saleModelFromSession.DiscountRatioByPercent;
            DiscountRatioFixed = saleModelFromSession.DiscountRatioFixed;
            IsFreeShipping = saleModelFromSession.IsFreeShipping;
            AllStoreInventoryItems = saleModelFromSession.AllStoreInventoryItems;
            StoreCategoryId = saleModelFromSession.StoreCategoryId;
            AllFixedPriceItems = saleModelFromSession.AllFixedPriceItems;
            AllAuctionItems = saleModelFromSession.AllAuctionItems;


        }
    }
}